package com.lc.luzhengtang.adapter;

import android.content.Context;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.drawable.ScalingUtils;
import com.facebook.drawee.generic.GenericDraweeHierarchy;
import com.facebook.drawee.generic.GenericDraweeHierarchyBuilder;
import com.facebook.drawee.interfaces.DraweeController;
import com.facebook.drawee.view.SimpleDraweeView;
import com.facebook.imagepipeline.common.ResizeOptions;
import com.facebook.imagepipeline.request.ImageRequest;
import com.facebook.imagepipeline.request.ImageRequestBuilder;
import com.lc.luzhengtang.R;
import com.zcx.helper.scale.ScaleScreenHelperFactory;

import java.util.ArrayList;
import java.util.List;

import static com.lc.luzhengtang.util.CommonUtils.getResources;

/**
 * Created by congl on 2017/7/21.
 */

public class CommentPicAdapter extends RecyclerView.Adapter<CommentPicAdapter.ViewHolder> {

    private Context context;
    private List<String> list;
    private OnItemClickListener listener;


    public CommentPicAdapter(Context context, OnItemClickListener listener) {
        this.context = context;
        this.listener = listener;
        list = new ArrayList<>();
    }

    public void setDatas(List<String> list) {
        this.list.clear();
        this.list.addAll(list);
        notifyDataSetChanged();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(((LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.item_comment_pics, parent, false));
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.loadDatas(position);
    }

    @Override
    public int getItemCount() {
        return list.size() == 0 ? 1 : list.size() == 4 ? 4 : list.size() + 1;
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        private SimpleDraweeView pic;

        public ViewHolder(View itemView) {
            super(itemView);
            ScaleScreenHelperFactory.getInstance().loadView(((ViewGroup) itemView));
            pic = ((SimpleDraweeView) itemView.findViewById(R.id.iv_pics));


        }

        public void loadDatas(int position) {
            String uri = "";

            GenericDraweeHierarchyBuilder builder =
                    new GenericDraweeHierarchyBuilder(context.getResources());

            if (list.size() == 0) {
                GenericDraweeHierarchy hierarchy = builder
                        .setFadeDuration(300)
                        .setActualImageScaleType(ScalingUtils.ScaleType.FIT_XY)
                        .build();
                pic.setHierarchy(hierarchy);
                uri = "res:///" + R.mipmap.tianjiatupian;
            } else if (position >= list.size()) {
                GenericDraweeHierarchy hierarchy = builder
                        .setFadeDuration(300)
                        .setActualImageScaleType(ScalingUtils.ScaleType.FIT_XY)
                        .build();
                pic.setHierarchy(hierarchy);
                uri = "res:///" + R.mipmap.tianjiatupian;
            } else {
                GenericDraweeHierarchy hierarchy = builder
                        .setFadeDuration(300)
                        .setActualImageScaleType(ScalingUtils.ScaleType.CENTER_CROP)
                        .build();
                pic.setHierarchy(hierarchy);
                String s = list.get(position);
                uri = "file://" + s;
            }

            ImageRequest request = ImageRequestBuilder.newBuilderWithSource(Uri.parse(uri))
                    .setResizeOptions(new ResizeOptions(120, 110))
                    .build();

            DraweeController controller = Fresco.newDraweeControllerBuilder()
                    .setOldController(pic.getController())
                    .setImageRequest(request)
                    .build();


            pic.setController(controller);


            pic.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    listener.onPicClick();
                }
            });
        }
    }

    public interface OnItemClickListener {
        void onPicClick();

        void onDeleteClick();
    }
}
